<!DOCTYPE html>
<html><head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
    <link rel="alternate" type="application/rss+xml" href="/redis/index.xml" title="h-book" />
    <link rel="canonical" href="/redis/">

    <title>
        
        Redis | h-book
        
    </title>

    
    <link href="/css/fontawesome.min.css" rel="stylesheet">

    
    <link rel="stylesheet" href="/css/ace.min.css">

    

    

</head>
<body><nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow sticky-top" id="navbarMain">
    <div class="container">
        <div>
            <a class="navbar-brand" href="/">
                
                h-book
            </a>
        </div>
    </div>
</nav>
<div class="container-fluid">
            <div class="row">

                <div class="docs-sidenav order-0 col-12 col-md-3 col-lg-2 col-xl-2 position-sticky border-right"><nav class="navbar navbar-expand-md navbar-light pl-0">
    <button class="navbar-toggler navbar-toggler-right collapsed" type="button" data-toggle="collapse" data-target="#sidenav-left-collapse" aria-controls="sidenav-left-collapse" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

     <div class="collapse navbar-collapse align-items-start flex-column" id="sidenav-left-collapse">
            <form class="form-inline my-2 my-lg-0 searchbox">
                <input class="form-control mr-sm-2 w-100" data-search-input id="search-by" type="text" placeholder="Search">
            </form>

        

         <ul class="navbar-nav flex-column pt-3">
    <li data-nav-id="/hugo/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/hugo/"><h6>Hugo</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/hugo/init/" class="nav-item my-1">
                
                
                  <a href="/hugo/init/" class="nav-link p-0">
                    init
                  </a>
                
        </li>
        <li data-nav-id="/hugo/add-page/" class="nav-item my-1">
                
                
                  <a href="/hugo/add-page/" class="nav-link p-0">
                    add-page
                  </a>
                
        </li>
        <li data-nav-id="/hugo/edit-skill/" class="nav-item my-1">
                
                
                  <a href="/hugo/edit-skill/" class="nav-link p-0">
                    edit-skill
                  </a>
                
        </li>
        <li data-nav-id="/hugo/publish/" class="nav-item my-1">
                
                
                  <a href="/hugo/publish/" class="nav-link p-0">
                    publish
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/android/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/android/"><h6>Android</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/android/install/" class="nav-item my-1">
                
                
                  <a href="/android/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/android/create-project/" class="nav-item my-1">
                
                
                  <a href="/android/create-project/" class="nav-link p-0">
                    create project
                  </a>
                
        </li>
        <li data-nav-id="/android/import-material/" class="nav-item my-1">
                
                
                  <a href="/android/import-material/" class="nav-link p-0">
                    import material
                  </a>
                
        </li>
        <li data-nav-id="/android/start-page/" class="nav-item my-1">
                
                
                  <a href="/android/start-page/" class="nav-link p-0">
                    startpage
                  </a>
                
        </li>
        <li data-nav-id="/android/i18n/" class="nav-item my-1">
                
                
                  <a href="/android/i18n/" class="nav-link p-0">
                    i18n
                  </a>
                
        </li>
        <li data-nav-id="/android/nav-flat-page/" class="nav-item my-1">
                
                
                  <a href="/android/nav-flat-page/" class="nav-link p-0">
                    nav flat page
                  </a>
                
        </li>
        <li data-nav-id="/android/activity/" class="nav-item my-1">
                
                
                  <a href="/android/activity/" class="nav-link p-0">
                    Activity
                  </a>
                
        </li>
        <li data-nav-id="/android/db/" class="nav-item my-1">
                
                
                  <a href="/android/db/" class="nav-link p-0">
                    DB
                  </a>
                
        </li>
        <li data-nav-id="/android/dialog/" class="nav-item my-1">
                
                
                  <a href="/android/dialog/" class="nav-link p-0">
                    Dialog
                  </a>
                
        </li>
        <li data-nav-id="/android/fragment/" class="nav-item my-1">
                
                
                  <a href="/android/fragment/" class="nav-link p-0">
                    Fragment
                  </a>
                
        </li>
        <li data-nav-id="/android/jetpack/" class="nav-item my-1">
                
                
                  <a href="/android/jetpack/" class="nav-link p-0">
                    Jetpack
                  </a>
                
        </li>
        <li data-nav-id="/android/material/" class="nav-item my-1">
                
                
                  <a href="/android/material/" class="nav-link p-0">
                    Material
                  </a>
                
        </li>
        <li data-nav-id="/android/storage/" class="nav-item my-1">
                
                
                  <a href="/android/storage/" class="nav-link p-0">
                    Storage
                  </a>
                
        </li>
        <li data-nav-id="/android/%E5%9B%BE%E6%A0%87/" class="nav-item my-1">
                
                
                  <a href="/android/%E5%9B%BE%E6%A0%87/" class="nav-link p-0">
                    图标
                  </a>
                
        </li>
        <li data-nav-id="/android/store/" class="nav-item my-1">
                
                
                  <a href="/android/store/" class="nav-link p-0">
                    Store
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/blender/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/blender/"><h6>Blender</h6></a>
        
    </li>
    <li data-nav-id="/bootstrap/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/bootstrap/"><h6>BootStrap</h6></a>
        
    </li>
    <li data-nav-id="/centos7/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/centos7/"><h6>Centos7</h6></a>
        
    </li>
    <li data-nav-id="/dlna/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/dlna/"><h6>DLNA</h6></a>
        
    </li>
    <li data-nav-id="/docker/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/docker/"><h6>Docker</h6></a>
        
    </li>
    <li data-nav-id="/dubbo/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/dubbo/"><h6>Dubbo</h6></a>
        
    </li>
    <li data-nav-id="/ffmpeg/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/ffmpeg/"><h6>FFmpeg</h6></a>
        
    </li>
    <li data-nav-id="/git/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/git/"><h6>Git</h6></a>
        
    </li>
    <li data-nav-id="/gitbook/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/gitbook/"><h6>Gitbook</h6></a>
        
    </li>
    <li data-nav-id="/gitlab/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/gitlab/"><h6>Gitlab</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/gitlab/runner/" class="nav-item my-1">
                
                
                  <a href="/gitlab/runner/" class="nav-link p-0">
                    Runner
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/gradle/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/gradle/"><h6>Gradle</h6></a>
        
    </li>
    <li data-nav-id="/java/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/java/"><h6>Java</h6></a>
        
    </li>
    <li data-nav-id="/jenkins/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/jenkins/"><h6>Jenkins</h6></a>
        
    </li>
    <li data-nav-id="/kubernetes/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/kubernetes/"><h6>Kubernetes</h6></a>
        
    </li>
    <li data-nav-id="/mybatis/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mybatis/"><h6>Mybatis</h6></a>
        
    </li>
    <li data-nav-id="/mybatis-plus/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mybatis-plus/"><h6>Mybatis Plus</h6></a>
        
    </li>
    <li data-nav-id="/mysql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mysql/"><h6>MySQL</h6></a>
        
    </li>
    <li data-nav-id="/nodejs/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/nodejs/"><h6>Node.js</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/nodejs/npm/" class="nav-item my-1">
                
                
                  <a href="/nodejs/npm/" class="nav-link p-0">
                    NPM
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/postgresql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/postgresql/"><h6>PostgreSQL</h6></a>
        
    </li>
    <li data-nav-id="/redis/" class="nav-item my-1 active
        ">
        
        
          <a class="nav-link p-0" href="/redis/"><h6>Redis</h6></a>
        
    </li>
    <li data-nav-id="/samba/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/samba/"><h6>Samba</h6></a>
        
    </li>
    <li data-nav-id="/sql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/sql/"><h6>SQL</h6></a>
        
    </li>
    <li data-nav-id="/ue5/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/ue5/"><h6>UE5</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/ue5/install/" class="nav-item my-1">
                
                
                  <a href="/ue5/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/ue5/uninstall/" class="nav-item my-1">
                
                
                  <a href="/ue5/uninstall/" class="nav-link p-0">
                    uninstall
                  </a>
                
        </li>
        <li data-nav-id="/ue5/qa/" class="nav-item my-1">
                
                
                  <a href="/ue5/qa/" class="nav-link p-0">
                    Q&amp;A
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity/"><h6>Unity</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity/install/" class="nav-item my-1">
                
                
                  <a href="/unity/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/unity/scene/" class="nav-item my-1">
                
                
                  <a href="/unity/scene/" class="nav-link p-0">
                    scene
                  </a>
                
        </li>
        <li data-nav-id="/unity/inspector/" class="nav-item my-1">
                
                
                  <a href="/unity/inspector/" class="nav-link p-0">
                    inspector
                  </a>
                
        </li>
        <li data-nav-id="/unity/assets/" class="nav-item my-1">
                
                
                  <a href="/unity/assets/" class="nav-link p-0">
                    assets
                  </a>
                
        </li>
        <li data-nav-id="/unity/audio-source/" class="nav-item my-1">
                
                
                  <a href="/unity/audio-source/" class="nav-link p-0">
                    audio source
                  </a>
                
        </li>
        <li data-nav-id="/unity/camera/" class="nav-item my-1">
                
                
                  <a href="/unity/camera/" class="nav-link p-0">
                    camera
                  </a>
                
        </li>
        <li data-nav-id="/unity/script/" class="nav-item my-1">
                
                
                  <a href="/unity/script/" class="nav-link p-0">
                    script
                  </a>
                
        </li>
        <li data-nav-id="/unity/prefabs/" class="nav-item my-1">
                
                
                  <a href="/unity/prefabs/" class="nav-link p-0">
                    prefabs
                  </a>
                
        </li>
        <li data-nav-id="/unity/physics/" class="nav-item my-1">
                
                
                  <a href="/unity/physics/" class="nav-link p-0">
                    physics
                  </a>
                
        </li>
        <li data-nav-id="/unity/skybox/" class="nav-item my-1">
                
                
                  <a href="/unity/skybox/" class="nav-link p-0">
                    skybox
                  </a>
                
        </li>
        <li data-nav-id="/unity/particle-system/" class="nav-item my-1">
                
                
                  <a href="/unity/particle-system/" class="nav-link p-0">
                    particle system
                  </a>
                
        </li>
        <li data-nav-id="/unity/shortcut-keys/" class="nav-item my-1">
                
                
                  <a href="/unity/shortcut-keys/" class="nav-link p-0">
                    shortcut keys
                  </a>
                
        </li>
        <li data-nav-id="/unity/qa/" class="nav-item my-1">
                
                
                  <a href="/unity/qa/" class="nav-link p-0">
                    Q&amp;A
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-2d-beginner/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-2d-beginner/"><h6>Unity 2D Beginner</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-2d-beginner/init-and-move/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/init-and-move/" class="nav-link p-0">
                    Init &amp; Move
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/map/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/map/" class="nav-link p-0">
                    Map
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/data/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/data/" class="nav-link p-0">
                    Data
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/animate/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/animate/" class="nav-link p-0">
                    Data
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/bullet/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/bullet/" class="nav-link p-0">
                    Bullet
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/cinemachine/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/cinemachine/" class="nav-link p-0">
                    Cinemachine
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/particle/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/particle/" class="nav-link p-0">
                    Particle
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/ui/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/ui/" class="nav-link p-0">
                    UI
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/npc/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/npc/" class="nav-link p-0">
                    NPC
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/sound/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/sound/" class="nav-link p-0">
                    Sound
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-2d-game-kit/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-2d-game-kit/"><h6>Unity 2D Game Kit</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-2d-game-kit/record/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-game-kit/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-3d-beginner/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-3d-beginner/"><h6>Unity 3D Beginner</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-3d-beginner/init/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/level/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/level/" class="nav-link p-0">
                    Level
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/nav/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/nav/" class="nav-link p-0">
                    Nav
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/render-pipeline/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/render-pipeline/" class="nav-link p-0">
                    Render Pipeline
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/postprocessing/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/postprocessing/" class="nav-link p-0">
                    PostProcessing
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/ui/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/ui/" class="nav-link p-0">
                    UI
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/enemy/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/enemy/" class="nav-link p-0">
                    Enemy
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/sound/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/sound/" class="nav-link p-0">
                    Sound
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/%E5%BB%BA%E6%A8%A1%E5%B7%A5%E5%85%B7/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/%E5%BB%BA%E6%A8%A1%E5%B7%A5%E5%85%B7/" class="nav-link p-0">
                    建模工具
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-arpg/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-arpg/"><h6>Unity ARPG</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-arpg/init/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-arpg/role-state-machine/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/role-state-machine/" class="nav-link p-0">
                    Role State Machine
                  </a>
                
        </li>
        <li data-nav-id="/unity-arpg/api-piece/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/api-piece/" class="nav-link p-0">
                    API Piece
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-animation/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-animation/"><h6>Unity Creativecore Animation</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-animation/door/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-animation/door/" class="nav-link p-0">
                    Door
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-animation/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-animation/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-lighting/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-lighting/"><h6>Unity Creativecore Lighting</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-lighting/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-lighting/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-lighting/outdoor/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-lighting/outdoor/" class="nav-link p-0">
                    Outdoor
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-shader/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-shader/"><h6>Unity Creativecore Shader</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-shader/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-shader/glass/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/glass/" class="nav-link p-0">
                    Glass
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-shader/shader-graph/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/shader-graph/" class="nav-link p-0">
                    Shader-Graph
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creator-kit-beginner-code/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creator-kit-beginner-code/"><h6>Unity Creator Kit Beginner Code</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creator-kit-beginner-code/record/" class="nav-item my-1">
                
                
                  <a href="/unity-creator-kit-beginner-code/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creator-kit-fps/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creator-kit-fps/"><h6>Unity Creator Kit FPS</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creator-kit-fps/record/" class="nav-item my-1">
                
                
                  <a href="/unity-creator-kit-fps/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-mythirdperson/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-mythirdperson/"><h6>Unity My Third Person</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-mythirdperson/init/" class="nav-item my-1">
                
                
                  <a href="/unity-mythirdperson/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-playground/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-playground/"><h6>Unity Playground</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-playground/record/" class="nav-item my-1">
                
                
                  <a href="/unity-playground/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-ui/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-ui/"><h6>Unity UI</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-ui/init/" class="nav-item my-1">
                
                
                  <a href="/unity-ui/init/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-vfx/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-vfx/"><h6>Unity VFX</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-vfx/init/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/init/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/bullet/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/bullet/" class="nav-link p-0">
                    bullet
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/rain/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/rain/" class="nav-link p-0">
                    rain
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/rock-rain/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/rock-rain/" class="nav-link p-0">
                    Rock Rain
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/vue/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/vue/"><h6>Vue</h6></a>
        
    </li>
    <li data-nav-id="/web/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/web/"><h6>Web</h6></a>
        
    </li>
        </ul>
    </div>
</nav>


</div>
                <div class="docs-toc large order-lg-2 order-md-0 order-xs-1 col-12 col-lg-2 col-xl-2 position-sticky border-left"><div class="docs-toc">
	<nav id="TableOfContents">
  <ul>
    <li><a href="#linux环境源码安装">Linux环境源码安装</a></li>
    <li><a href="#docker版">Docker版</a>
      <ul>
        <li><a href="#下载">下载</a></li>
        <li><a href="#启动">启动</a></li>
        <li><a href="#配置">配置</a></li>
        <li><a href="#k8s启停">K8S启、停</a></li>
      </ul>
    </li>
    <li><a href="#压力测试工具redis-benchmark">压力测试工具redis-benchmark</a></li>
    <li><a href="#redis-cli基本命令">redis-cli基本命令</a></li>
    <li><a href="#数据类型">数据类型</a>
      <ul>
        <li><a href="#string">string</a></li>
        <li><a href="#list">list</a></li>
        <li><a href="#set">set</a></li>
        <li><a href="#hash">hash</a></li>
        <li><a href="#zset">zset</a></li>
        <li><a href="#geospatial">geospatial</a></li>
        <li><a href="#hyperloglog">hyperloglog</a></li>
        <li><a href="#bitmap">bitmap</a></li>
      </ul>
    </li>
    <li><a href="#事务">事务</a></li>
    <li><a href="#watch">watch</a></li>
    <li><a href="#jedis">jedis</a></li>
    <li><a href="#spring-boot">spring boot</a></li>
    <li><a href="#发布订阅">发布订阅</a></li>
    <li><a href="#主从复制">主从复制</a></li>
    <li><a href="#哨兵模式">哨兵模式</a></li>
    <li><a href="#缓存穿透">缓存穿透</a></li>
    <li><a href="#缓存击穿">缓存击穿</a></li>
    <li><a href="#缓存雪崩">缓存雪崩</a></li>
  </ul>
</nav>
</div>
</div>
                <div class="main col-12 order-1 col-md-9 col-lg-10 col-xl-8 py-3">
                

<div class="d-flex flex-column">
    <h1 class="js-title">Redis</h1>
    <div class="d-flex align-items-center">
        
    </div>
</div>

<hr>


<p><a href="https://redis.io/">https://redis.io/</a></p>
<p>remote dictionary server 远程字典服务</p>
<p>所有数据都在内存中，单线程操作最佳</p>
<h2 id="linux环境源码安装">Linux环境源码安装</h2>
<p>官网下源码包</p>
<p>yum install gcc-c++</p>
<p>make</p>
<p>make install</p>
<p>默认安装路径在/usr/local/bin</p>
<p>拷贝源码目录下的redis.conf到安装目录，按需改配置，如daemonize后台运行</p>
<p>redis-server redis.conf</p>
<p>redis-cli -p 6379</p>
<p>ping</p>
<p>keys *</p>
<h2 id="docker版">Docker版</h2>
<p><a href="https://hub.docker.com/_/redis">https://hub.docker.com/_/redis</a></p>
<h3 id="下载">下载</h3>
<p>docker pull redis:5.0.9-alpine</p>
<h3 id="启动">启动</h3>
<ul>
<li>start a redis instance</li>
</ul>
<p>docker run &ndash;name redis -d redis:5.0.9-alpine</p>
<ul>
<li>start with persistent storage</li>
</ul>
<p>docker run -v /root/redis:/data &ndash;name redis -d redis:5.0.9-alpine redis-server &ndash;appendonly yes</p>
<ul>
<li>use your own redis.conf</li>
</ul>
<p>docker run -v /root/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf &ndash;name redis -d redis:5.0.9-alpine redis-server /usr/local/etc/redis/redis.conf</p>
<h3 id="配置">配置</h3>
<p><a href="https://redis.io/documentation">https://redis.io/documentation</a></p>
<p><a href="https://www.cnblogs.com/wudequn/p/8059412.html">https://www.cnblogs.com/wudequn/p/8059412.html</a></p>
<p>The self documented redis.conf for Redis X.Y</p>
<p>如https://raw.githubusercontent.com/antirez/redis/5.0.9/redis.conf</p>
<p>大小写不敏感</p>
<p>include /mypah/sub.conf # 包含其它配置文件</p>
<p>bind 0.0.0.0 # 允许非本机的访问</p>
<p>daemonize yes # 后台启动</p>
<p>pidfile /var/run/redis_6379.pid # 后台运行时需要指定pid文件</p>
<p>loglevel notice # 日志级别</p>
<p>logfile &quot;&quot; # 日志文件</p>
<p>databases 16 # 默认16个数据库</p>
<p>save 900 1 # 900秒内，至少有一个key进行了修改，就会持久化</p>
<p>stop-writes-on-bgsave-error yes #  持久化时出错还会继续工作</p>
<p>dir ./ # rdb文件保存的目录</p>
<p>requirepass mypassword # 设置秘密</p>
<p>maxclients 10000 # 最大客户端连接数量</p>
<p>maxmemory <code>&lt;bytes&gt;</code> # 最大内存</p>
<p>maxmemory-policy noeviction # 内存上限后的处理策略，有6中，比如支队过期时间的key进行LRU（默认）、随机删除、删除即将过期的、永不过期返回错误</p>
<p>appendonly no # 默认不开启aof，也就是默认用rdb</p>
<p>appendfsync no|everysec|always # 不执行同步、每秒同步、每次修改同步</p>
<h4 id="rdb">rdb</h4>
<p>指定时间间隔内将内存中的数据快照写入磁盘，恢复时是将快照文件直接读到内存</p>
<p>redis会单独创建一个子进程来持久化，会先将数据写入到一个临时文件中，待持久化过程结束了，再用这个临时文件替换上次持久化好的文件。整个过程中，主进程不进行任何IO操作。如果需要进行大规模数据的恢复，且对于数据恢复的完整性不是非常敏感，RDB的方式比AOF更高效。缺点就是最后一次持久化的数据可能丢失</p>
<p>主从复制中，rdb是备用的，建议从机上用rdb，只保留save 900 1的设置</p>
<h4 id="aof">aof</h4>
<p>以日志的形式来记录每一个操作，读不记录，当文件写到设置大小后（默认64M，建议5G以上），会写重写文件。redis启动时会读取这个文件，并执行一遍</p>
<p>redis-check-aof &ndash;fix appendonly.aof 可以修复错误的aof文件</p>
<p>一般aof更新频率高，所以优先aof还原数据库。若开启了aof持久化支持，appendfsync默认每秒执行一次。no：不会主动fsync，只会由操作系统在需要的时候刷新数据。always：在每次写入后执行fsync</p>
<p>两种都开启时，AOF优先</p>
<h3 id="k8s启停">K8S启、停</h3>
<p><a href="./redis.conf">redis.conf</a></p>
<ul>
<li>注释掉bind。因为bind配置了什么ip，别人就得访问bind里面配置的ip才访问到redis服务，而127.0.0.1是本机。bind 0.0.0.0等价于 不配置bind，即注释掉bind</li>
<li>protected-mode yes改为no</li>
<li>去掉requirepass的注释并修改后面的字符串为自己的密码，如ESP496z8</li>
</ul>
<p><a href="./redis.yml">redis.yml</a></p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cmd" data-lang="cmd"><span style="display:flex;"><span><span style="color:#00a8c8">cd</span> /root/hcindy/redis
</span></span><span style="display:flex;"><span>kubectl create configmap redis-config --from-file=redis.conf
</span></span><span style="display:flex;"><span>kubectl get configmaps redis-config -o yaml <span style="color:#111">&gt;</span> redis-config.yml
</span></span><span style="display:flex;"><span>kubectl apply -f redis.yml
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">cd</span> /root/hcindy/redis
</span></span><span style="display:flex;"><span>kubectl delete -f redis.yml
</span></span><span style="display:flex;"><span>kubectl delete -f redis-config.yml
</span></span><span style="display:flex;"><span>rm -rf /root/hcindy/hostpath/redis
</span></span></code></pre></div><h2 id="压力测试工具redis-benchmark">压力测试工具redis-benchmark</h2>
<p>-h 指定机器</p>
<p>-p 指定端口</p>
<p>-c 指定并发连接数，默认50</p>
<p>-n 指定请求数，默认10000，即每个连接请求一万次</p>
<h2 id="redis-cli基本命令">redis-cli基本命令</h2>
<p>查官网</p>
<p>切第三个数据库 select 3</p>
<p>查看数据库大小 dbsize</p>
<p>清当前库 flushdb</p>
<p>清全部库 flushall</p>
<p>增string set <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>查 get <code>&lt;key&gt;</code></p>
<p>判断存在 exists <code>&lt;key&gt;</code></p>
<p>删 move <code>&lt;key&gt;</code> 1</p>
<p>设10s后过期 expire <code>&lt;key&gt;</code> 10</p>
<p>查生存时间 ttl <code>&lt;key&gt;</code></p>
<p>判类型 type <code>&lt;key&gt;</code></p>
<h2 id="数据类型">数据类型</h2>
<h3 id="string">string</h3>
<p>给当前对应key的字符串添加字符串 append <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>取长度 strlen <code>&lt;key&gt;</code></p>
<p>自增1(set <code>&lt;key&gt;</code> 0) incr <code>&lt;key&gt;</code></p>
<p>自减1(set <code>&lt;key&gt;</code> 0) decr <code>&lt;key&gt;</code></p>
<p>按步长自增 incrby <code>&lt;key&gt;</code> <code>&lt;step&gt;</code></p>
<p>按步长自减 decrby <code>&lt;key&gt;</code> <code>&lt;step&gt;</code></p>
<p>按闭区间取子串 getrange <code>&lt;key&gt;</code> <code>&lt;start&gt;</code> <code>&lt;end&gt;</code></p>
<p>按偏移替换那一位字符 setrange <code>&lt;key&gt;</code> <code>&lt;offset&gt;</code> <code>&lt;value&gt;</code></p>
<p>存在时set setex <code>&lt;key&gt;</code> <code>&lt;second&gt;</code> <code>&lt;value&gt;</code></p>
<p>不存在时set setnx <code>&lt;key&gt;</code> <code>&lt;second&gt;</code> <code>&lt;value&gt;</code></p>
<p>批量设置 mset <code>&lt;key&gt;</code> <code>&lt;value&gt;</code> <code>&lt;key&gt;</code> <code>&lt;value&gt;</code> &hellip;</p>
<p>批量获取 mget <code>&lt;key&gt;</code> <code>&lt;key&gt;</code> &hellip;</p>
<p>原子性的批量存在时set msetex</p>
<p>原子性的批量不存在时set msetnx</p>
<p>设置“对象” set user:1 {name:zhang, age:2} 或者 mset user:1:name zhang user:1:age 2</p>
<p>先get再set(会返回上一个值或空) getset <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<h3 id="list">list</h3>
<p>插入到头 lpush <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>查 lrange <code>&lt;key&gt;</code> 0 -1</p>
<p>插入到尾 rpush <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>从头弹出 lpop <code>&lt;key&gt;</code></p>
<p>从尾弹出 rpop <code>&lt;key&gt;</code></p>
<p>按索引（1）获取值 lindex <code>&lt;key&gt;</code> 1</p>
<p>获得长度 llen <code>&lt;key&gt;</code></p>
<p>按值和数量移除 lrem <code>&lt;key&gt;</code> <code>&lt;count&gt;</code> <code>&lt;value&gt;</code></p>
<p>取start到end闭区间子集合 ltrim <code>&lt;key&gt;</code> <code>&lt;start&gt;</code> <code>&lt;stop&gt;</code></p>
<p>从source尾弹出到dest集合中 rpoplpush <code>&lt;source&gt;</code> <code>&lt;dest&gt;</code></p>
<p>指定索引赋值（不存在时报错） lset <code>&lt;key&gt;</code> <code>&lt;index&gt;</code> <code>&lt;value&gt;</code></p>
<p>指定值（value1）前/后插入 linsert <code>&lt;key&gt;</code> before/after <code>&lt;value1&gt;</code> <code>&lt;value2&gt;</code></p>
<h3 id="set">set</h3>
<p>增 sadd <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>全查询 smembers <code>&lt;key&gt;</code></p>
<p>判存在 sismember <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>查数量 scard <code>&lt;key&gt;</code></p>
<p>删指定值 srem <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>随机取指定个数的值 srandmember <code>&lt;key&gt;</code> <code>&lt;count&gt;</code></p>
<p>随机移除1个 spop <code>&lt;key&gt;</code></p>
<p>从source将value移动到dest中 smove <code>&lt;source&gt;</code> <code>&lt;dest&gt;</code> <code>&lt;value&gt;</code></p>
<p>差集 sdiff <code>&lt;key1&gt;</code> <code>&lt;key2&gt;</code></p>
<p>交集 sinter <code>&lt;key1&gt;</code> <code>&lt;key2&gt;</code></p>
<p>并集 sunion <code>&lt;key1&gt;</code> <code>&lt;key2&gt;</code></p>
<h3 id="hash">hash</h3>
<p>增 hset <code>&lt;key&gt;</code> <code>&lt;k&gt;</code> <code>&lt;v&gt;</code></p>
<p>查 hget <code>&lt;key&gt;</code> <code>&lt;k&gt;</code></p>
<p>批量增 hmset <code>&lt;key&gt;</code> <code>&lt;k1&gt;</code> <code>&lt;v1&gt;</code> <code>&lt;k2&gt;</code> <code>&lt;v2&gt;</code></p>
<p>全查询（结果是k1 v1 k2 v2） hgetall <code>&lt;key&gt;</code></p>
<p>删指定<code>&lt;k&gt;``&lt;v&gt;</code> hdel <code>&lt;key&gt;</code> <code>&lt;k&gt;</code></p>
<p>查数量 hlen <code>&lt;key&gt;</code></p>
<p>判存在 hexists <code>&lt;key&gt;</code> <code>&lt;k&gt;</code></p>
<p>获取所有k hkeys <code>&lt;key&gt;</code></p>
<p>指定自增 hincrby <code>&lt;key&gt;</code> <code>&lt;k&gt;</code> <code>&lt;count&gt;</code></p>
<p>指定自减 hdecrby <code>&lt;key&gt;</code> <code>&lt;k&gt;</code> <code>&lt;count&gt;</code></p>
<p>不存在就设置 hsetnx <code>&lt;key&gt;</code> <code>&lt;k&gt;</code> <code>&lt;v&gt;</code></p>
<h3 id="zset">zset</h3>
<p>有序(score)集合。-inf负无穷 +inf正无穷</p>
<p>指定score增 zadd <code>&lt;key&gt;</code> <code>&lt;score&gt;</code> <code>&lt;value&gt;</code></p>
<p>全查询 zrange <code>&lt;key&gt;</code> 0 -1</p>
<p>最小值到最大值的升排序 zrangebyscore <code>&lt;key&gt;</code> -inf +inf</p>
<p>降序 zrevrange <code>&lt;key&gt;</code> 0 -1</p>
<p>删指定 zrem <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>查数量 zcard <code>&lt;key&gt;</code></p>
<p>按区间查数量 zcount <code>&lt;key&gt;</code> <code>&lt;min&gt;</code> <code>&lt;max&gt;</code></p>
<h3 id="geospatial">geospatial</h3>
<p>3.2版本推出。zset实现</p>
<p>南北两极不能添加，有效经度-180~180，有效纬度-85.05112878~85.05112878，单位m米 km千米 mi英里 ft英尺</p>
<p>添加地点(value)的经(v1)纬(v2)度 geoadd <code>&lt;key&gt;</code> <code>&lt;v1&gt;</code> <code>&lt;v2&gt;</code> <code>&lt;value&gt;</code></p>
<p>查 geopos <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>两地直线距离 geodist  <code>&lt;key&gt;</code> <code>&lt;value1&gt;</code> <code>&lt;value2&gt;</code> <code>&lt;unit&gt;</code></p>
<p>返回一个或多个位置元素的geohash(字符串)(具体查官网) geohash <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<p>以给定的经(v1)纬(v2)度为中心，找出某一半径内的元素 georadius <code>&lt;key&gt;</code> <code>&lt;v1&gt;</code> <code>&lt;v2&gt;</code> <code>&lt;radius&gt;</code> m|km|mi|ft withdist withcoord count <code>&lt;count&gt;</code></p>
<p>以地点(value)为中心，找指定范围的元素 georadiusbymember <code>&lt;key&gt;</code> <code>&lt;value&gt;</code> <code>&lt;radius&gt;</code> m|km|mi|ft withcoord</p>
<p>全查 zrange <code>&lt;key&gt;</code> 0 -1</p>
<p>删 zrem <code>&lt;key&gt;</code> <code>&lt;value&gt;</code></p>
<h3 id="hyperloglog">hyperloglog</h3>
<p>2.8.9版本。基数统计算法。占用内存固定。2^64不同的元素的基数占12KB内存。0.81%的错误率</p>
<p>网页的UV（一人访问一个网站多次，但只算一个人）</p>
<p>增 pfadd <code>&lt;key&gt;</code> <code>&lt;v1&gt;</code> <code>&lt;v2&gt;</code> <code>&lt;v3&gt;</code></p>
<p>不重复的个数 pfcount <code>&lt;key&gt;</code></p>
<p>并集 pfmerge <code>&lt;destkey&gt;</code> <code>&lt;sourcekey&gt;</code></p>
<h3 id="bitmap">bitmap</h3>
<p>365天 = 365bit 约等于46byte（字节） 1byte = 8bit</p>
<p>按位增 setbit <code>&lt;key&gt;</code> <code>&lt;offset&gt;</code> <code>&lt;value&gt;</code></p>
<p>按位查 getbit <code>&lt;key&gt;</code> <code>&lt;offset&gt;</code></p>
<p>计数1的位 bitcount <code>&lt;key&gt;</code> [start] [end]</p>
<h2 id="事务">事务</h2>
<p>单条命令具备原子性，但事务不保证且没有隔离</p>
<p>multi</p>
<p>&hellip;之间的命令将会进入队列，直到执行exec，或者用discard放弃事务</p>
<p>exec</p>
<ul>
<li>队列中存在错误命令（不存在的命令）时，该错误会抛异常，最后没有预期结果</li>
<li>队列中存在不正确使用命令（如对字符串进行自增），不会影响正确的命令，且正确命令部分会生效</li>
</ul>
<h2 id="watch">watch</h2>
<p>乐观锁</p>
<p>事务成功后，监控会自动取消</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cmd" data-lang="cmd"><span style="display:flex;"><span><span style="color:#00a8c8">set</span> <span style="color:#111">money 100</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">set</span> <span style="color:#111">out 0</span>
</span></span><span style="display:flex;"><span>watch money
</span></span><span style="display:flex;"><span>multi
</span></span><span style="display:flex;"><span>decrby money 20
</span></span><span style="display:flex;"><span>incrby out 20
</span></span><span style="display:flex;"><span>exec
</span></span></code></pre></div><p>如果在事务期间，被watch的有发生变化（其它线程操作），该事务就会失败，需要unwatch解锁</p>
<h2 id="jedis">jedis</h2>
<p>采用直连，多个线程操作不安全，需要用jedis pool连接池</p>
<p>redis.clients:jedis:3.2.0</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">void</span> <span style="color:#75af00">t</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">Jedis</span> <span style="color:#111">jedis</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">Jedis</span><span style="color:#111">(</span><span style="color:#d88200">&#34;127.0.0.1&#34;</span><span style="color:#111">,</span> <span style="color:#111">6379</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">System</span><span style="color:#111">.</span><span style="color:#75af00">out</span><span style="color:#111">.</span><span style="color:#75af00">println</span><span style="color:#111">(</span><span style="color:#111">jedis</span><span style="color:#111">.</span><span style="color:#75af00">ping</span><span style="color:#111">());</span> <span style="color:#75715e">// pong</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// api名字和命令同名同义</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 事务</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">Transaction</span> <span style="color:#111">t</span> <span style="color:#f92672">=</span> <span style="color:#111">jedis</span><span style="color:#111">.</span><span style="color:#75af00">multi</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">jedis</span><span style="color:#111">.</span><span style="color:#75af00">watch</span><span style="color:#111">(</span><span style="color:#d88200">&#34;u1&#34;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">try</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">t</span><span style="color:#111">.</span><span style="color:#75af00">set</span><span style="color:#111">(</span><span style="color:#d88200">&#34;u1&#34;</span><span style="color:#111">,</span> <span style="color:#d88200">&#34;1&#34;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">t</span><span style="color:#111">.</span><span style="color:#75af00">set</span><span style="color:#111">(</span><span style="color:#d88200">&#34;u2&#34;</span><span style="color:#111">,</span> <span style="color:#d88200">&#34;2&#34;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">t</span><span style="color:#111">.</span><span style="color:#75af00">set</span><span style="color:#111">(</span><span style="color:#d88200">&#34;u3&#34;</span><span style="color:#111">,</span> <span style="color:#d88200">&#34;3&#34;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">t</span><span style="color:#111">.</span><span style="color:#75af00">exec</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span> <span style="color:#00a8c8">catch</span> <span style="color:#111">(</span><span style="color:#111">Exception</span> <span style="color:#111">e</span><span style="color:#111">)</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">t</span><span style="color:#111">.</span><span style="color:#75af00">discard</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span> <span style="color:#00a8c8">finally</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">jedis</span><span style="color:#111">.</span><span style="color:#75af00">close</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><h2 id="spring-boot">spring boot</h2>
<p>2.x后是用的lettuce，底层是netty，不存在线程不安全的情况</p>
<p>spring-boot-autoconfigure-2.x.y.jar中的meta-inf/spring.factories下有RedisAutoConfiguration类，进而能找到ReisProperties配置类（官网手册也有写）</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-txt" data-lang="txt"><span style="display:flex;"><span>spring.redis.lettuce.pool.max-active = 8
</span></span><span style="display:flex;"><span>spring.redis.lettuce.pool.max-idle = 8
</span></span><span style="display:flex;"><span>spring.redis.lettuce.pool.max-wait = -1ms
</span></span><span style="display:flex;"><span>spring.redis.lettuce.pool.min-idle = 0
</span></span><span style="display:flex;"><span>spring.redis.lettuce.pool.time-between-eviction-runs
</span></span></code></pre></div><p>默认有RedisTemplat<code>&lt;Object, Object&gt;</code>和StringRedisTemplate两个bean，前者是当没自定义时才会被创建。</p>
<p>当value存对象进去的时候，可能报没有序列化的错误，可以在对象上implements Serializable接口</p>
<p>默认序列化使用的JdkSerializationRedisSerializer</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// 自定义序列化</span>
</span></span><span style="display:flex;"><span><span style="color:#75af00">@Bean</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#111">RedisTemplate</span><span style="color:#f92672">&lt;</span><span style="color:#111">String</span><span style="color:#111">,</span> <span style="color:#111">Object</span><span style="color:#f92672">&gt;</span> <span style="color:#75af00">redisTemplate</span><span style="color:#111">(</span><span style="color:#111">RedisConnectionFactory</span> <span style="color:#111">redisConnectionFactory</span><span style="color:#111">)</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">RedisTemplate</span><span style="color:#f92672">&lt;</span><span style="color:#111">String</span><span style="color:#111">,</span> <span style="color:#111">Object</span><span style="color:#f92672">&gt;</span> <span style="color:#111">template</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">RedisTemplate</span><span style="color:#f92672">&lt;&gt;</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// json 序列化</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">Jackson2JsonRedisSerializer</span> <span style="color:#111">jackson2JsonRedisSerializer</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">Jackson2JsonRedisSerializer</span><span style="color:#111">(</span><span style="color:#111">Object</span><span style="color:#111">.</span><span style="color:#75af00">class</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">ObjectMapper</span> <span style="color:#111">om</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">ObjectMapper</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">om</span><span style="color:#111">.</span><span style="color:#75af00">setVisibility</span><span style="color:#111">(</span><span style="color:#111">PropertyAccessor</span><span style="color:#111">.</span><span style="color:#75af00">ALL</span><span style="color:#111">,</span> <span style="color:#111">JsonAutoDetect</span><span style="color:#111">.</span><span style="color:#75af00">Visibility</span><span style="color:#111">.</span><span style="color:#75af00">ANY</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">om</span><span style="color:#111">.</span><span style="color:#75af00">enableDefaultTyping</span><span style="color:#111">(</span><span style="color:#111">ObjectMapper</span><span style="color:#111">.</span><span style="color:#75af00">DefaultTyping</span><span style="color:#111">.</span><span style="color:#75af00">NON_FINAL</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">jackson2JsonRedisSerializer</span><span style="color:#111">.</span><span style="color:#75af00">setObjectMapper</span><span style="color:#111">(</span><span style="color:#111">om</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// string 序列化</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">StringRedisSerializer</span> <span style="color:#111">stringRedisSerializer</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">StringRedisSerializer</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// key序列化方式</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">template</span><span style="color:#111">.</span><span style="color:#75af00">setKeySerializer</span><span style="color:#111">(</span><span style="color:#111">stringRedisSerializer</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// hash key序列化方式</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">template</span><span style="color:#111">.</span><span style="color:#75af00">setHashKeySerializer</span><span style="color:#111">(</span><span style="color:#111">stringRedisSerializer</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// value序列化方式</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">template</span><span style="color:#111">.</span><span style="color:#75af00">setValueSerializer</span><span style="color:#111">(</span><span style="color:#111">jackson2JsonRedisSerializer</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// hash value序列化方式</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">template</span><span style="color:#111">.</span><span style="color:#75af00">setHashValueSerializer</span><span style="color:#111">(</span><span style="color:#111">jackson2JsonRedisSerializer</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">template</span><span style="color:#111">.</span><span style="color:#75af00">afterPropertiesSet</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#111">template</span><span style="color:#111">.</span><span style="color:#75af00">setConnectionFactory</span><span style="color:#111">(</span><span style="color:#111">redisConnectionFactory</span><span style="color:#111">)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">return</span> <span style="color:#111">template</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#00a8c8">class</span> <span style="color:#75af00">A</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75af00">@Autowired</span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">private</span> <span style="color:#111">RedisTempla</span> <span style="color:#111">redisTempla</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#00a8c8">void</span> <span style="color:#75af00">t</span><span style="color:#111">()</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">RedisConnection</span> <span style="color:#111">connection</span> <span style="color:#f92672">=</span> <span style="color:#111">redisTempla</span><span style="color:#111">.</span><span style="color:#75af00">getConnectionFactory</span><span style="color:#111">().</span><span style="color:#75af00">getConnection</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">connection</span><span style="color:#111">.</span><span style="color:#75af00">flushDb</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>        <span style="color:#111">connection</span><span style="color:#111">.</span><span style="color:#75af00">flushAll</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// redisTempla.opsForXxx // Cluster、Geo、Hash、HyperLogLog、List、Set、Stream、Value、ZSet</span>
</span></span><span style="display:flex;"><span>        
</span></span><span style="display:flex;"><span>    <span style="color:#111">}</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><h2 id="发布订阅">发布订阅</h2>
<p>订阅者订阅（会保持连接） subscribe <code>&lt;channel&gt;</code></p>
<p>发布者发布 publish <code>&lt;channel&gt;</code> &lt;&ldquo;message&rdquo;&gt;</p>
<h2 id="主从复制">主从复制</h2>
<p>单台redis最大使用内存不应该超过20G</p>
<p>一台（主）redis服务器的数据，复制给其他（从）redis服务器，数据复制是单向的，主写从读</p>
<p>默认情况下每台redis服务器都是主。一主可以有多从，一从只能有一主</p>
<ul>
<li>数据冗余</li>
<li>故障恢复</li>
<li>负载均衡</li>
<li>高可用</li>
</ul>
<p>单服务器测试集群的话，修改配置文件后，启动多个程序</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>port &lt;port&gt;
</span></span><span style="display:flex;"><span>pidfile /var/run/redis_&lt;port&gt;.pid
</span></span><span style="display:flex;"><span>logfile &#34;&lt;port&gt;.log&#34;
</span></span><span style="display:flex;"><span>dbfilename dump&lt;port&gt;.rdb
</span></span></code></pre></div><p>cli从机暂时设定主机 slaveof <code>&lt;host&gt;</code> <code>&lt;port&gt;</code></p>
<p>配置文件永久设定主机 replicaof <code>&lt;host&gt;</code> <code>&lt;port&gt;</code></p>
<p>cli查看当前库的信息 info replication</p>
<p>cli切回主 slaveof no one</p>
<p>主从模式下，主掉后，从不会自己选举。从掉后恢复会被主同步，新的从机会被全量复制</p>
<p>特殊主从：a(主) &lt;- b(从、主) &lt;- c（从）。在a上写数据，b、c都查到，但b实际状态为从，所以不能在b上写数据</p>
<h2 id="哨兵模式">哨兵模式</h2>
<p>2.8提供了sentinel哨兵功能</p>
<p>哨兵是一个独立的进程，并且哨兵也可以是集群</p>
<ul>
<li>哨兵发送命令，让redis服务器返回运行状态，包括主从，对其进行监控</li>
<li>哨兵检查到主挂，会将一个从改成主，然后通过 发布订阅模式 通知其它从，修改配置文件，使其切主</li>
<li>集群的哨兵还会互相监控</li>
<li>集群的哨兵判主挂是建立在一定数量哨兵都发现后，再随机由一个哨兵发起投票</li>
</ul>
<p>3哨兵1主2从</p>
<p>哨兵配置文件 sentinel.conf ，更多配置项参看官网</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-txt" data-lang="txt"><span style="display:flex;"><span>port &lt;port&gt;
</span></span><span style="display:flex;"><span># &lt;name&gt; 随便写，人为可读。1表示会进行投票
</span></span><span style="display:flex;"><span>sentinel monitor &lt;name&gt; &lt;host&gt; &lt;port&gt; 1
</span></span></code></pre></div><p>启动哨兵 redis-sentinel sentinel.conf</p>
<p>主挂后，会被哨兵选主。主回来后，会降为从</p>
<p>缺点在于不好进行在线扩容，集群容量一旦达到上限，就很麻烦</p>
<h2 id="缓存穿透">缓存穿透</h2>
<p>大量地查不存在的数据（缓存没有，查数据库）</p>
<p>此时可以将这个查询+空结果存短生存期的缓存，也可以加 布隆过滤器（一种数据结构，对有所可能查询的参数以hash形式存储），在控制层先进行校验，不符合则丢弃，从而避免对底层存储系统的破坏</p>
<h2 id="缓存击穿">缓存击穿</h2>
<p>一个key非常热点，大并发都集中访问这个点，当key失效时，会查到数据库</p>
<p>设定永不过期，或者使用分布式锁，保证每个key同时只有一个线程取查后端，没有获得锁的线程等待，此时分布式锁的考验很大</p>
<h2 id="缓存雪崩">缓存雪崩</h2>
<p>某个时间段，缓存集中过期或服务器宕机</p>
<ul>
<li>高可用防止集体宕机（异地多活）</li>
<li>限流降级。当缓存失效后，通过加锁或者队列来控制读数据库写缓存的线程数量</li>
<li>数据预热</li>
</ul>


    


                    
                    <div class="row"></div> 

                </div>

            </div> 

        </div> 
<script src="/lib/jquery.min.js"></script> 
<script src="/lib/popper.min.js"></script> 

<script src="/js/bootstrap.min.js"></script> 


<script type="text/javascript" src="/plugins/lunr.min.js"></script>
<script type="text/javascript" src="/plugins/auto-complete.js"></script>
<link href="/plugins/auto-complete.css" rel="stylesheet">
<script type="text/javascript">
  
      var baseurl = "\/";
  
</script>
<script type="text/javascript" src="/plugins/search.js"></script>

<script type="text/javascript" src="/plugins/favorites.js"></script>


<script type="text/javascript" src="/plugins/clipboard.js"></script>
<script>
  new ClipboardJS('.btn');
</script>
</body>
</html>
